*for REStat paper, "Breastfeeding and Children's Early Cognitive Outcomes."

libname dd 'D:\data\';


DATA x;
set dd.kindz2;
keep i_id bfeed kage kreadt kmatht kreads kmaths bf0 bf1 bf23 bf45 bf6p
bf1s bf23s bf45s01 bf45s23 bf45s01 bf45s23 bf6ps01 bf6ps23 bf6ps45 female 
twin asian chinese amerind black hispanic
bwtvl bwtlow marbirth agebirth momed prenat smoke regne regnc regs regw
foreign border1 border2 csect gestlt35 gest3537 r1ptest prework
backw0 backw13 backw46 backw6p khhinc wicp wicpdv bfathed dadeddv dadocc doccdv
wkr0 smokedv csectdv r1ptestdv regdv gestdv bwtdv agedv pworkdv backwdv 
quart pwork0 pwork16 pwork712 roft12 roft3 roft4 roftdv i_twinpr bfrate law bfmo;

*round 1 data;  
bfeed = .;
if (p1brstfd = 1) then bfeed = 1;
if (p1brstfd = 2) then bfeed = 0;

bfmo = .;
if p1brstlg ge 0 then bfmo = p1brstlg;

**want to know if at least 6  months, so can do some filling in;
if (bfeed = 1 and bfmo = .) then do;
    if P1BRSTNW = 1 then bfmo = 6;
end;
if (bfeed = . and y2momtyp = 1 and y2rsprel = 1 and p2evbrfd = 2) then bfeed = 0;

**now form bf measure as series of dvs;
*not breastfed;
bf0 = .;
*breastfed 0, 1 months;
bf1 = .;
*breastfed 2, 3 months;
bf23 = .;
*breastfed 4,5 months;
bf45 = .;
*breastfed 6 or more months;
bf6p = .;
if bfeed = 0 then do;
   bf0 = 1;
   bf1 = 0;
   bf23 = 0;
   bf45 = 0;
   bf6p = 0;
end;
if bfeed = 1 then do;
if (bfmo = 0 or bfmo = 1) then do;
   bf0 = 0;
   bf1 = 1;
   bf23 = 0;
   bf45 = 0;
   bf6p = 0;
end;
if (bfmo = 2 or bfmo = 3) then do;
   bf0 = 0;
   bf1 = 0;
   bf23 = 1;
   bf45 = 0;
   bf6p = 0;
end;
if (bfmo = 4 or bfmo = 5) then do;
   bf0 = 0;
   bf1 = 0;
   bf23 = 0;
   bf45 = 1;
   bf6p = 0;
end;
if (bfmo ge 6) then do;
   bf0 = 0;
   bf1 = 0;
   bf23 = 0;
   bf45 = 0;
   bf6p = 1;
end;
end;

 *now, see if supplemented;
bf1s = .;
if bf1 = 1 then do;
   if (P1AGFORM = 0 or p1agmilk = 0 or p1agfood = 0 ) then bf1s = 1;
   else if (p1agform = 995 or p1agform ge 1) then bf1s = 0; 
end;
bf23s = .;
if bf23 = 1 then do;
   if (P1AGFORM = 0 or p1agmilk = 0 or p1agfood = 0 or p1agform = 1 or p1agmilk = 1 or p1agfood = 1) then bf23s = 1;
   else if (p1agform = 995 or p1agform ge 2) then bf23s = 0; 
end;
bf45s01 = .;
bf45s23 = .;
if bf45 = 1 then do;
   if (P1AGFORM = 0 or p1agmilk = 0 or p1agfood = 0 or p1agform = 1 or p1agmilk = 1 or p1agfood = 1) then bf45s01 = 1;
   else if (p1agform = 995 or p1agform ge 2) then bf45s01 = 0; 
   if (P1AGFORM = 2 or p1agmilk = 2 or p1agfood = 2 or p1agform = 3 or p1agmilk = 3 or p1agfood = 3) then bf45s23 = 1;
   else if (p1agform = 995 or p1agform ge 4) then bf45s23 = 0; 
   if bf45s01 = 1 then bf45s23 = 0;
end;
bf6ps01 = .;
bf6ps23 = .;
bf6ps45 = .;
if bf6p = 1 then do;
   if (P1AGFORM = 0 or p1agmilk = 0 or p1agfood = 0 or p1agform = 1 or p1agmilk = 1 or p1agfood = 1) then bf6ps01 = 1;
   else if (p1agform = 995 or p1agform ge 2) then bf6ps01 = 0; 
   if (P1AGFORM = 2 or p1agmilk = 2 or p1agfood = 2 or p1agform = 3 or p1agmilk = 3 or p1agfood = 3) then bf6ps23 = 1;
   else if (p1agform = 995 or p1agform ge 4) then bf6ps23 = 0; 
   if bf6ps01 = 1 then bf6ps23 = 0;
   if (P1AGFORM = 4 or p1agmilk = 4 or p1agfood = 4 or p1agform = 5 or p1agmilk = 5 or p1agfood = 5) then bf6ps45 = 1;
   else if (p1agform = 995 or p1agform ge 6) then bf6ps45 = 0; 
   if bf6ps01 = 1 then bf6ps45 = 0;
   if bf6ps23 = 1 then bf6ps45 = 0;

   **these guys presently bf children, and haven't fed milk or formula in past 7 days;
   if (p1form7d = 2 and p1milk7d = 2) then do;
   if bf6ps01 = . then bf6ps01 = 0;
   if bf6ps23 = . then bf6ps23 = 0;
   if bf6ps45 = . then bf6ps45 = 0;
   end;
end;

*let's just define some basic information about the child;
female = .;
if x4chsex = 2 then female = 1;
if x4chsex = 1 then female = 0;

**oversample chars;
twin = 0;
asian = 0;
chinese = 0;
amerind = 0;
if x1twsamp = 1 then twin = 1;
if x1asismp = 1 then asian = 1;
if x1chnsmp = 1 then chinese = 1;
if x1aisamp = 1 then amerind = 1;

*this is mother's race, for the most part;
black = .;
hispanic = .;
if (bcmomhsp ge 1 and bcmomhsp le 4) then hispanic = 1;
if bcmomhsp = 0 then hispanic = 0;
if hispanic = 1 then black = 0;
if (hispanic = 0 and bcmomrc = 2) then black = 1;
if (hispanic = 0 and bcmomrc = 1) then black = 0;
if (hispanic = 0 and bcmomrc ge 3 and bcmomrc le 14) then black = 0; 

if (hispanic = . and black = .) then do;
   if (y1hmrace = 7 or y1hmrace = 5 or y1hmrace = 1) then black = 0;
   if (y1hmrace = 7 or y1hmrace = 5 or y1hmrace = 1) then hispanic = 0;
   if (y1hmrace = 3 or y1hmrace = 4) then black = 0;
   if (y1hmrace = 3 or y1hmrace = 4) then hispanic = 1;
   if (y1hmrace = 2) then black = 1;
   if (y1hmrace = 2) then hispanic = 0;
   if (y1hmrace = 8 and y1chrace = 1) then black = 0;
   if (y1hmrace = 8 and y1chrace = 1) then hispanic = 0;
   if (y1hmrace = 8 and y1chrace = 3) then black = 0;
   if (y1hmrace = 8 and y1chrace = 3) then hispanic = 1;
   if (y1hmrace = 8 and y1chrace = 4) then black = 0;
   if (y1hmrace = 8 and y1chrace = 4) then hispanic = 1;
   if (y1hmrace = 8 and y1chrace = 7) then black = 0;
   if (y1hmrace = 8 and y1chrace = 7) then hispanic = 0;
end;
   if (hispanic = . and black = .) then do;
       if asian = 1 then hispanic = 0;
	   if asian = 1 then black = 0;
	   if amerind = 1 then hispanic = 0;
	   if amerind = 1 then black = 0;
   end;
**leaves only 1 uncoded;

**what if instead did child's race, how different;
   cblack = .;
   chisp = .;
   if (y1chrace = 1 or y1chrace = 5 or y1chrace = 6 or y1chrace = 7) then cblack = 0;
   if (y1chrace = 1 or y1chrace = 5 or y1chrace = 6 or y1chrace = 7) then chisp = 0;
   if y1chrace = 2 then cblack = 1;
   if y1chrace = 2 then chisp = 0;
   if y1chrace = 3 then cblack = 0;
   if y1chrace = 3 then chisp = 1;
   if y1chrace = 4 then cblack = 0;
   if y1chrace = 4 then chisp = 1;
   if (y1chrace = 8 and black = 0 and hispanic = 0) then cblack = 0;
   if (y1chrace = 8 and black = 0 and hispanic = 0) then chisp = 0;
   if (y1chrace = 8 and black = 1 and hispanic = 0) then cblack = 1;
   if (y1chrace = 8 and black = 1 and hispanic = 0) then chisp = 0;
   if (y1chrace = 8 and black = 0 and hispanic = 1) then cblack = 0;
   if (y1chrace = 8 and black = 0 and hispanic = 1) then chisp = 1;
   if (y1chrace = -9 and black = 0 and hispanic = 0) then cblack = 0;
   if (y1chrace = -9 and black = 0 and hispanic = 0) then chisp = 0;
   if (y1chrace = -9 and black = 1 and hispanic = 0) then cblack = 1;
   if (y1chrace = -9 and black = 1 and hispanic = 0) then chisp = 0;
   if (y1chrace = -9 and black = 0 and hispanic = 1) then cblack = 0;
   if (y1chrace = -9 and black = 0 and hispanic = 1) then chisp = 1;
**definitely differs, took mother's from birth certificate;

*(vl)very low birthweight is less than 1500 grams, or 3 pounds 5 ounces;
**low)low birthweight is less than 2500 grams, or 5 pounds 8 ounces;
bwtvl = .;
bwtlow = .;
if x1bthwgt = 1 then bwtvl = 0;
if x1bthwgt = 2 then bwtvl = 0;
if x1bthwgt = 3 then bwtvl = 1;

if x1bthwgt = 1 then bwtlow = 0;
if x1bthwgt = 2 then bwtlow = 1;
if x1bthwgt = 3 then bwtlow = 0;

**next, let's look at some birth certificate information;
*married at birth;
marbirth = .;
if bcmommar = 1 then marbirth = 1;;
if bcmommar = 2 then marbirth = 0;

if marbirth = . then do;
  if p1marsts = 5 then marbirth = 0;
  **deleted hand edit ids;
  if (p1marsts = 1 and p1nummrd = 1 and p1yrmrry ge 1980 and p1yrmrry le 2000) then marbirth = 1;
end;
*that is all of them filled in for the sample I'm using;

*mom's age at birth;
agebirth = .;
if bcmomage gt 0 then agebirth = bcmomage;

*mother years of education, multiple possible variables;
momed = .;
if (bcmomed ge 0 and bcmomed le 24) then momed = bcmomed;
*they revised this, into intervals, so will just have to take in-between numbers;
if momed = . then do;
   if y1momed = 1 then momed = 4;
   if y1momed = 2 then momed = 10.5;
   if y1momed = 3 then momed = 12;
   if y1momed = 4 then momed = 12;
   if y1momed = 5 then momed = 14;
   if y1momed = 6 then momed = 16;
   if y1momed = 7 then momed = 17;
   if y1momed = 8 then momed = 17;
   if y1momed = 9 then momed = 17;
   **to be consistent with bc question, 5 or more years of college;
end;

*more birth certificate information;
*prenatal care in first trimester;
prenat = .;
if ((bcmonpre = 0) or (bcmonpre ge 4 and bcmonpre le 9)) then prenat = 0;
if (bcmonpre = 1 or bcmonpre = 2 or bcmonpre = 3) then prenat = 1; 

*fill in more from questionnaire;
if prenat = . then do;
   if p1visdoc = 2 then prenat = 0;
   if P1NMVS1T = 0 then prenat = 0;
   if P1NMVS1T ge 1 then prenat = 1;
end;

*smoke during pregnancy, note that California does not report this on birth certificate;
smoke = .;
if bctobacc = 1 then smoke = 1;
if bctobacc = 2 then smoke = 0;

*region of birth residence;
regne = .;
regnc = .;
regs = .;
regw = .;

if (bcstrsfp = 4 or bcstrsfp = 8 or bcstrsfp = 16 or bcstrsfp = 30 or bcstrsfp = 32 or bcstrsfp = 35
  or bcstrsfp = 49 or bcstrsfp = 56 or bcstrsfp = 2 or bcstrsfp = 6 or bcstrsfp = 15 or bcstrsfp = 41 
  or bcstrsfp = 53) then regw = 1;

if (bcstrsfp = 10 or bcstrsfp = 11 or bcstrsfp = 12 or bcstrsfp = 13 or bcstrsfp = 24 or bcstrsfp = 37
  or bcstrsfp = 45 or bcstrsfp = 51 or bcstrsfp = 54 or bcstrsfp = 1 or bcstrsfp = 21 or bcstrsfp = 28 
  or bcstrsfp = 47 or bcstrsfp = 5 or bcstrsfp = 22 or bcstrsfp = 40 or bcstrsfp = 48) then regs = 1;

if (bcstrsfp = 17 or bcstrsfp = 18 or bcstrsfp = 26 or bcstrsfp = 39 or bcstrsfp = 55 or bcstrsfp = 19
  or bcstrsfp = 20 or bcstrsfp = 27 or bcstrsfp = 29 or bcstrsfp = 31 or bcstrsfp = 38 or bcstrsfp = 46) 
  then regnc = 1;

 if (bcstrsfp = 9 or bcstrsfp = 23 or bcstrsfp = 25 or bcstrsfp = 33 or bcstrsfp = 44 or bcstrsfp = 50
  or bcstrsfp = 34 or bcstrsfp = 36 or bcstrsfp = 42) 
  then regne = 1;

*region of birth residence;
if regne = 1 then do;
regs = 0;
regw = 0;
regnc = 0;
end;

if regs = 1 then do;
regne = 0;
regw = 0;
regnc = 0;
end;

if regw = 1 then do;
regs = 0;
regne = 0;
regnc = 0;
end;

if regnc = 1 then do;
regs = 0;
regw = 0;
regne = 0;
end;
*thought about filling in with household round 1 region, but could have moved by then;

*foreign born;
foreign = .;
if BCMBRPLC = 1 then foreign = 0;
if BCMBRPLC = 2 then foreign = 1;

**see if can fill in for missing;
if foreign = . then do;
   if p2momrel = 1 and p2ctrybn = 1 then foreign = 0;
   else if p2momrel = 1 and p2ctrybn = 3 then foreign = 1;
   else if p3momrel = 1 and p3ctbn_r = 1 then foreign = 0;
end;

**birth order;
border1 = .;
border2 = .;
if bcnlbnl = 0 then border1 = 1;
if bcnlbnl = 0 then border2 = 0;
if bcnlbnl = 1 then border1 = 0;
if bcnlbnl = 1 then border2 = 1;
if bcnlbnl ge 2 and bcnlbnl le 20 then border1 = 0;
if bcnlbnl ge 2 and bcnlbnl le 20 then border2 = 0;
if (border1 = .) then do;
   if p1numch = 1 then border1 = 1;
   if p1numch = 1 then border2 = 0;
   if (p1numch = 2) then border1 = 0;
   if (p1numch = 2) then border2 = 1;
   if (p1numch ge 3) then border1 = 0;
   if (p1numch ge 3) then border2 = 0;
end;
if (border1 = .) then do;
   if y1numsib = 0 then border1 = 1;
   if y1numsib = 0 then border2 = 0;
   if y1numsib = 1 then border1 = 0;
   if y1numsib = 1 then border2 = 1;
   if y1numsib = 2 then border1 = 0;
   if y1numsib = 2 then border2 = 0;
end;

*c-section delivery;
csect = .;
if (bcprimac = 1 or bcrep_c = 1) then csect = 1;
else if (bcprimac = 2 and bcrep_c = 2) then csect = 0;

*apgar score from birth certificate;
apgar = .;
if (bcfmaps ge 0 and bcfmaps le 10) then apgar = bcfmaps;

*weeks of gestation;
gestlt35 = .;
gest3537 = .;
if (bcgestwk gt 0 and bcgestwk lt 35) then gestlt35 = 1;
if (bcgestwk gt 0 and bcgestwk lt 35) then gest3537 = 0;
if (bcgestwk ge 35 and bcgestwk le 37) then gestlt35 = 0;
if (bcgestwk ge 35 and bcgestwk le 37) then gest3537 = 1;
if (bcgestwk ge 38 and bcgestwk le 50) then gestlt35 = 0;
if (bcgestwk ge 38 and bcgestwk le 50) then gest3537 = 0;

*round 1 parent test of sensitivity;
r1ptest = .;
if x1ncattp ne -9 then r1ptest = x1ncattp; 

*number of months work 12 months prior to child's birth;
prework = .;
if p1wkbf12 = 2 then prework = 0;
if p1wkbf12 = 1 and p1nmhrwk gt 0 then prework = p1nmhrwk;

*I'm going to change this to a series of dummy variables;
pwork0 = .;
pwork16 = .;
pwork712 = .;
if prework = 0 then do;
pwork0 = 1;
pwork16 = 0;
pwork712 = 0;
end;
if (prework ge 1 and prework le 6) then do;
pwork0 = 0;
pwork16 = 1;
pwork712 = 0;
end;
if (prework ge 7 and prework le 12) then do;
pwork0 = 0;
pwork16 = 0;
pwork712 = 1;
end;

*go back to work (first round);
backw = .;
if p1bckwrk = 2 then backw = 0;
if (p1bkwku = 2 and p1bkwknm gt 0) then backw = p1bkwknm;
if (p1bkwku = 1 and p1bkwknm gt 0) then backw = p1bkwknm/4.29;

backw0 = .;
backw13 = .;
backw46 = .;
backw6p = .;
if backw = 0 then do;
backw0 = 1;
backw13 = 0;
backw46 = 0;
backw6p = 0;
end;
if (backw gt 0 and backw le 3) then do;
backw0 = 0;
backw13 = 1;
backw46 = 0;
backw6p = 0;
end;
if (backw gt 3 and backw le 6) then do;
backw0 = 0;
backw13 = 0;
backw46 = 1;
backw6p = 0;
end;
if (backw gt 6) then do;
backw0 = 0;
backw13 = 0;
backw46 = 0;
backw6p = 1;
end;

**household income, note taking midpoint;
r1hhinc = .;
if x1income = 1 then r1hhinc = 2500;
if x1income = 2 then r1hhinc = 7500;
if x1income = 3 then r1hhinc = 12500;
if x1income = 4 then r1hhinc = 17500;
if x1income = 5 then r1hhinc = 22500;
if x1income = 6 then r1hhinc = 27500;
if x1income = 7 then r1hhinc = 32500;
if x1income = 8 then r1hhinc = 37500;
if x1income = 9 then r1hhinc = 45000;
if x1income = 10 then r1hhinc = 62500;
if x1income = 11 then r1hhinc = 87500;
if x1income = 12 then r1hhinc = 150000;
if x1income = 13 then r1hhinc = 200000;
r1hhinc = log(r1hhinc);

r2hhinc = .;
if x2income = 1 then r2hhinc = 2500;
if x2income = 2 then r2hhinc = 7500;
if x2income = 3 then r2hhinc = 12500;
if x2income = 4 then r2hhinc = 17500;
if x2income = 5 then r2hhinc = 22500;
if x2income = 6 then r2hhinc = 27500;
if x2income = 7 then r2hhinc = 32500;
if x2income = 8 then r2hhinc = 37500;
if x2income = 9 then r2hhinc = 45000;
if x2income = 10 then r2hhinc = 62500;
if x2income = 11 then r2hhinc = 87500;
if x2income = 12 then r2hhinc = 150000;
if x2income = 13 then r2hhinc = 200000;
r2hhinc = log(r2hhinc);

r3hhinc = .;
if x3income = 1 then r3hhinc = 2500;
if x3income = 2 then r3hhinc = 7500;
if x3income = 3 then r3hhinc = 12500;
if x3income = 4 then r3hhinc = 17500;
if x3income = 5 then r3hhinc = 22500;
if x3income = 6 then r3hhinc = 27500;
if x3income = 7 then r3hhinc = 32500;
if x3income = 8 then r3hhinc = 37500;
if x3income = 9 then r3hhinc = 45000;
if x3income = 10 then r3hhinc = 62500;
if x3income = 11 then r3hhinc = 87500;
if x3income = 12 then r3hhinc = 150000;
if x3income = 13 then r3hhinc = 200000;
r3hhinc = log(r3hhinc);

**will only be using this, for the most part;
r4hhinc = .;
if x4income = 1 then r4hhinc = 2500;
if x4income = 2 then r4hhinc = 7500;
if x4income = 3 then r4hhinc = 12500;
if x4income = 4 then r4hhinc = 17500;
if x4income = 5 then r4hhinc = 22500;
if x4income = 6 then r4hhinc = 27500;
if x4income = 7 then r4hhinc = 32500;
if x4income = 8 then r4hhinc = 37500;
if x4income = 9 then r4hhinc = 45000;
if x4income = 10 then r4hhinc = 62500;
if x4income = 11 then r4hhinc = 87500;
if x4income = 12 then r4hhinc = 150000;
if x4income = 13 then r4hhinc = 200000;
r4hhinc=r4hhinc*.496;
r4hhinc = log(r4hhinc);

r5hhinc = .;
if x5income = 1 then r5hhinc = 2500;
if x5income = 2 then r5hhinc = 7500;
if x5income = 3 then r5hhinc = 12500;
if x5income = 4 then r5hhinc = 17500;
if x5income = 5 then r5hhinc = 22500;
if x5income = 6 then r5hhinc = 27500;
if x5income = 7 then r5hhinc = 32500;
if x5income = 8 then r5hhinc = 37500;
if x5income = 9 then r5hhinc = 45000;
if x5income = 10 then r5hhinc = 62500;
if x5income = 11 then r5hhinc = 87500;
if x5income = 12 then r5hhinc = 150000;
if x5income = 13 then r5hhinc = 200000;
r5hhinc = r5hhinc*.482;
r5hhinc = log(r5hhinc);

**now test scores;
*round 1;
**t-score;
r1scoret = .;
if x1rmtlt ne -99 then r1scoret = x1rmtlt; 
**scale score;
r1scores = .;
if x1rmtls ne -9 then r1scores = x1rmtls; 

*round 2;
**t-score;
r2scoret = .;
if x2mtltsc ne -99 then r2scoret = x2mtltsc; 
**scale score;
r2scores = .;
if x2mtlscl ne -9 then r2scores = x2mtlscl; 

*round 3;
*scale score;
r3reads = .;
if x3rscr2 ne -9 then r3reads = x3rscr2;
*theta score;
r3readt = .;
if x3rthr2 ne -9 then r3readt = x3rthr2;

*scale score;
r3maths = .;
if x3mscr2 ne -9 then r3maths = x3mscr2;
*theta score;
r3matht = .;
if x3mthr2 ne -9 then r3matht = x3mthr2;

*round 4;
*scale score;
r4reads = .;
if x4rscr2 ne -9 then r4reads = x4rscr2;
*theta score;
r4readt = .;
if x4rthr2 ne -9 then r4readt = x4rthr2;

*scale score;
r4maths = .;
if x4mscr2 ne -9 then r4maths = x4mscr2;
*theta score;
r4matht = .;
if x4mthr2 ne -9 then r4matht = x4mthr2;

*round 5;
*scale score;
r5reads = .;
if x5rscr2 ne -9 then r5reads = x5rscr2;
*theta score;
r5readt = .;
if x5rthr2 ne -9 then r5readt = x5rthr2;

*scale score;
r5maths = .;
if x5mscr2 ne -9 then r5maths = x5mscr2;
*theta score;
r5matht = .;
if x5mthr2 ne -9 then r5matht = x5mthr2;

*assessment age;
r1age = .;
r2age = .;
r3age = .;
r4age = .;
r5age = .;
if x1asage gt 0 then r1age = x1asage;
if x2asage gt 0 then r2age = x2asage;
if x3asage gt 0 then r3age = x3asage;
if x4asage gt 0 then r4age = x4asage;
if x5asage gt 0 then r5age = x5asage;

r4kind = .;
r5kind = .;
if xkkdata = 1 then r4kind = 1;
if xkkdata = 2 then r5kind = 1;

**how often read;
roft12 = .;
roft3 = .;
roft4 = .;
if p4momrel = 1 then do;
if r4kind = 1 then do;
   if p4readbo = 1 then do;
   roft12 = 1;
   roft3 = 0;
   roft4 = 0;
   end;
   if p4readbo = 2 then do;
   roft12 = 1;
   roft3 = 0;
   roft4 = 0;
   end;
   if p4readbo = 3 then do;
   roft12 = 0;
   roft3 = 1;
   roft4 = 0;
   end;
   if p4readbo = 4 then do;
   roft12 = 0;
   roft3 = 0;
   roft4 = 1;
   end;
end;
end;
if p5momrel = 1 then do;
if r5kind = 1 then do;
   if p5readbo = 1 then do;
   roft12 = 1;
   roft3 = 0;
   roft4 = 0;
   end;
   if p5readbo = 2 then do;
   roft12 = 1;
   roft3 = 0;
   roft4 = 0;
   end;
   if p5readbo = 3 then do;
   roft12 = 0;
   roft3 = 1;
   roft4 = 0;
   end;
   if p5readbo = 4 then do;
   roft12 = 0;
   roft3 = 0;
   roft4 = 1;
   end;
end;
end;

**okay, let's focus on the kindergarten scores for now;
kmaths = .;
if r4kind = 1 then kmaths = r4maths;
if r5kind = 1 then kmaths = r5maths;
kreads = .;
if r4kind = 1 then kreads = r4reads;
if r5kind = 1 then kreads = r5reads;

kmatht = .;
if r4kind = 1 then kmatht = r4matht;
if r5kind = 1 then kmatht = r5matht;
kreadt = .;
if r4kind = 1 then kreadt = r4readt;
if r5kind = 1 then kreadt = r5readt;

kage = .;
if r4kind = 1 then kage = r4age;
if r5kind = 1 then kage = r5age;

khhinc = .;
if r4kind = 1 then khhinc = r4hhinc;
if r5kind = 1 then khhinc = r5hhinc;

**try to add some more variables;
**wic while pregnant;
wicp = .;
if p1wicbft = 2 then wicp = 0;
if (p1wicbft = 1 and p1wicprg = 1) then wicp = 1;
if (p1wicbft = 1 and p1wicprg = 2) then wicp = 0;

*I want to know if resident father in hh at 9 month interview was there at the child's birth;
resf9b = .;
if (Y1FTHTYP = 1 and p1marsts = 1 and marbirth = 1) then resf9b = 1;
else if (Y1FTHTYP = 1 and p1marsts = 5 and P1LVPARY ge 1980 and P1LVPARY le 2000) then resf9b = 1;
else if (Y1FTHTYP = 1 and p1marsts = 5 and P1LVPARY = 2002) then resf9b = 0;
else if y1fthtyp = 1 and p1marsts = 5 and P1LVPARY = 2001 and P1LVPARm gt 0 then do;
   if P1LVPARm gt bcdobmm then resf9b = 0;
   if P1LVPARm le bcdobmm then resf9b = 1;
end;
else if (y1fthtyp = 1 and p1marsts = 1 and p1lvpary ge 1980 and p1lvpary le 2000) then resf9b = 1;
else if (y1fthtyp = 1 and p1marsts = 1 and p1lvpary = 2002) then resf9b = 0;
else if y1fthtyp = 1 and p1marsts = 1 and P1LVPARY = 2001 and P1LVPARm gt 0 then do;
   if P1LVPARm gt bcdobmm then resf9b = 0;
   if P1LVPARm le bcdobmm then resf9b = 1;
end;
else if (y1fthtyp = 1 and resf9b = . and p1lvpary ge 1980 and p1lvpary le 2000) then resf9b = 1;
else if (y1fthtyp = 1 and resf9b = . and p1lvpary = 2002) then resf9b = 0;
else if (y1fthtyp = 1 and resf9b = . and P1LVPARY = 2001 and P1LVPARm gt 0) then do;
   if P1LVPARm gt bcdobmm then resf9b = 0;
   if P1LVPARm le bcdobmm then resf9b = 1;
end;

*fill in a few extras;
  **deleted ids;
  
 *try to calculate biological father's education;
   if y1fthtyp = 1 then do;
   if y1fthed = 1 then bfathed = 4;
   if y1fthed = 2 then bfathed = 10.5;
   if y1fthed = 3 then bfathed = 12;
   if y1fthed = 4 then bfathed = 12;
   if y1fthed = 5 then bfathed = 14;
   if y1fthed = 6 then bfathed = 16;
   if y1fthed = 7 then bfathed = 17;
   if y1fthed = 8 then bfathed = 17;
   if y1fthed = 9 then bfathed = 17;
end;
  if bfathed = . and p1bfeduc ge 0 then bfathed = p1bfeduc;
  if bfathed = . and y1fthtyp = 1 and p1speduc ge 0 then bfathed = p1speduc;
  if bfathed = . and n1highgr ge 0 then bfathed = n1highgr;
  if bfathed gt 17 then bfathed = 17;

  if bfathed = . and y2fthtyp = 1 then do;
   if y2fthed = 1 then bfathed = 4;
   if y2fthed = 2 then bfathed = 10.5;
   if y2fthed = 3 then bfathed = 12;
   if y2fthed = 4 then bfathed = 12;
   if y2fthed = 5 then bfathed = 14;
   if y2fthed = 6 then bfathed = 16;
   if y2fthed = 7 then bfathed = 17;
   if y2fthed = 8 then bfathed = 17;
   if y2fthed = 9 then bfathed = 17;
end;
  if bfathed = . and y3fthtyp = 1 then do;
   if y3fthed = 1 then bfathed = 4;
   if y3fthed = 2 then bfathed = 10.5;
   if y3fthed = 3 then bfathed = 12;
   if y3fthed = 4 then bfathed = 12;
   if y3fthed = 5 then bfathed = 14;
   if y3fthed = 6 then bfathed = 16;
   if y3fthed = 7 then bfathed = 17;
   if y3fthed = 8 then bfathed = 17;
   if y3fthed = 9 then bfathed = 17;
end;
**pretty good, 137  missing;

*occupational prestige score, bio dad resident;
dadocc = .;
if y1fthscr ge 0 and resf9b = 1 then dadocc = y1fthscr;
 
**bio dad self report, ever arrested;
bdadarr = .;
if f1relch = 1 then do;
   if f1arrest = 1 then bdadarr = 1;
   if f1arrest = 2 then bdadarr = 0;
end; 

**rate of breastfeeding in state in 2001;
if bcstrsfp = 1 then bfrate = 51.8;
if bcstrsfp = 2 then bfrate = 88.3;
if bcstrsfp = 4 then bfrate = 80.1;
if bcstrsfp = 5 then bfrate = 58.2;
if bcstrsfp = 6 then bfrate = 81.7;
if bcstrsfp = 8 then bfrate = 84.8;
if bcstrsfp = 9 then bfrate = 69.4;
if bcstrsfp = 10 then bfrate = 68.2;
if bcstrsfp = 11 then bfrate = 69.1;
if bcstrsfp = 12 then bfrate = 69.6;
if bcstrsfp = 13 then bfrate = 61.9;
if bcstrsfp = 15 then bfrate = 88.5;
if bcstrsfp = 16 then bfrate = 87.3;
if bcstrsfp = 17 then bfrate = 65.2;
if bcstrsfp = 18 then bfrate = 63.5;
if bcstrsfp = 19 then bfrate = 68.4;
if bcstrsfp = 20 then bfrate = 73.8;
if bcstrsfp = 21 then bfrate = 54;
if bcstrsfp = 22 then bfrate = 51.1;
if bcstrsfp = 23 then bfrate = 73.5;
if bcstrsfp = 24 then bfrate = 67.8;
if bcstrsfp = 25 then bfrate = 70.9;
if bcstrsfp = 26 then bfrate = 64.3;
if bcstrsfp = 27 then bfrate = 79.1;
if bcstrsfp = 28 then bfrate = 50.4;
if bcstrsfp = 29 then bfrate = 66.1;
if bcstrsfp = 30 then bfrate = 87.1;
if bcstrsfp = 31 then bfrate = 71.6;
if bcstrsfp = 32 then bfrate = 74.5;
if bcstrsfp = 33 then bfrate = 74.7;
if bcstrsfp = 34 then bfrate = 65.2;
if bcstrsfp = 35 then bfrate = 82.7;
if bcstrsfp = 36 then bfrate = 64.8;
if bcstrsfp = 37 then bfrate = 65;
if bcstrsfp = 38 then bfrate = 74;
if bcstrsfp = 39 then bfrate = 62.4;
if bcstrsfp = 40 then bfrate = 69.8;
if bcstrsfp = 41 then bfrate = 88.1;
if bcstrsfp = 42 then bfrate = 61.7;
if bcstrsfp = 44 then bfrate = 63.2;
if bcstrsfp = 45 then bfrate = 57.7;
if bcstrsfp = 46 then bfrate = 74.9;
if bcstrsfp = 47 then bfrate = 59.2;
if bcstrsfp = 48 then bfrate = 69.4;
if bcstrsfp = 49 then bfrate = 86.6;
if bcstrsfp = 50 then bfrate = 79.1;
if bcstrsfp = 51 then bfrate = 67;
if bcstrsfp = 53 then bfrate = 87.2;
if bcstrsfp = 54 then bfrate = 58.1;
if bcstrsfp = 55 then bfrate = 70.1;
if bcstrsfp = 56 then bfrate = 81.3;

law = .;
if (bcstrsfp = 1 or bcstrsfp = 5 or bcstrsfp = 4 or bcstrsfp = 8 or bcstrsfp = 11 or bcstrsfp = 18
or bcstrsfp = 20 or bcstrsfp = 21 or bcstrsfp = 25 or bcstrsfp = 28 or bcstrsfp = 38 or bcstrsfp = 31 
or bcstrsfp = 39 or bcstrsfp = 40 or bcstrsfp = 42 or bcstrsfp = 45 or bcstrsfp = 54 or bcstrsfp = 56
or bcstrsfp = 46 or bcstrsfp = 50)
then law = 0;
if (bcstrsfp = 2 or bcstrsfp = 6 or bcstrsfp = 9 or bcstrsfp = 10 or bcstrsfp = 12 or bcstrsfp = 13
  or bcstrsfp = 15 or bcstrsfp = 19 or bcstrsfp = 16 or bcstrsfp = 17 or bcstrsfp = 22 or bcstrsfp = 24
  or bcstrsfp = 23 or bcstrsfp = 26 or bcstrsfp = 27 or bcstrsfp = 29 or bcstrsfp = 30 or bcstrsfp = 37
  or bcstrsfp = 33 or bcstrsfp = 34 or bcstrsfp = 35 or bcstrsfp = 32 or bcstrsfp = 36 or bcstrsfp = 41
  or bcstrsfp = 44 or bcstrsfp = 47 or bcstrsfp = 48 or bcstrsfp = 49 or bcstrsfp = 51 or bcstrsfp = 53
  or bcstrsfp = 55) then law = 1;


**create dummy variables;
smokedv = 0;
if smoke = . then smokedv = 1;
if smokedv = 1 then smoke = .;

csectdv = 0;
if csect = . then csectdv = 1;
if csectdv = 1 then csect = .;

r1ptestdv = 0;
if r1ptest = . then r1ptestdv = 1;
if r1ptestdv = 1 then r1ptest = .;

regdv = 0;
if regne = . then regdv = 1;
if regdv = 1 then do;
regne = .;
regs = .;
regw = .;
regnc = .;
end;

gestdv = 0;
if gestlt35 = . then gestdv = 1;
if gestdv = 1 then do;
gestlt35 = .;
gest3537 = .;
end;

bwtdv = 0;
if bwtvl = . then bwtdv = 1;
if bwtdv = 1 then do;
bwtvl = .;
bwtlow = .;
end;

agedv = 0;
if (agebirth = .) then agedv = 1;

pworkdv = 0;
if prework = . then pworkdv = 1;
if pworkdv = 1 then do;
prework = .;
pwork0 = .;
pwork16 = .;
pwork712 = .;
end;

backwdv = 0;
if backw0 = . then backwdv = 1;
if backwdv = 1 then do;
backw0 = .;
backw13 = .;
backw46 = .;
backw6p = .;
end;

wicpdv = 0;
if wicp = . then wicpdv = 1;

dadeddv = 0;
if bfathed = . then dadeddv = 1;

doccdv = 0;
if dadocc = . then doccdv = 1;

roftdv = 0;
if roft4 = . then roftdv = 1;


**(NOTE, had starred out deletions and outputed SCORES data set, with id, quarter of birth, kindergarten weight, and kindergarten test scores,
**kmatht, kreadt, wkr0, etc.);

**deletions;
if bf0 ne .;
*birth mother is round 1 respondent;
if p1prntrs = 1;
*no weight for kindergarten;
if wkr0 ne .;

*need test scores;
if kmaths ne . and kreads ne .;

**only missing one observation each;
if black ne .;
if prenat ne .;
if foreign ne .;

**for norms by quarter of birth;
quart = .;
if (BCDOBMM = 1 or BCDOBMM = 2 or BCDOBMM = 3) then quart = 1;
if (BCDOBMM = 4 or BCDOBMM = 5 or BCDOBMM = 6) then quart = 2;
if (BCDOBMM = 7 or BCDOBMM = 8 or BCDOBMM = 9) then quart = 3;
if (BCDOBMM = 10 or BCDOBMM = 11 or BCDOBMM = 12) then quart = 4;

if (kmatht ne . or kreadt ne .);

proc sort;  by i_id;

data y;
set dd.scores2;
proc means;
run;
proc sort;  by i_id;

data dd.kind2;
merge x y;  by i_id;
if bf0 ne .;

proc means;
run;
proc contents;
run;
